/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Server;

import CLIENT.Client;
import CLIENT.Message;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Yaniv Kalif
 */
public class ServerMessageListener implements Runnable
{
    Client _client;
    // in=null;
    ObjectInputStream in = null;
    ServerMainChatPanel _CreatorP;
    public ServerMessageListener(Client client,ServerMainChatPanel Creator) throws IOException
    {
        _client = client;
        //in = new BufferedReader(new InputStreamReader(_client.GetSocket().getInputStream()));
        in = new ObjectInputStream(client.GetSocket().getInputStream());
        _CreatorP=Creator;
    }
    public void run()
    {
        try 
        {
            String msg = null;
            Message m = null;
            try {
                /*
                while ((msg = in.readLine()) != null)
                {
                if(in != null)
                {
                System.out.println(""+_client.GetName()+":" + msg);
                _CreatorP.SendMessage(_client, msg);// send the message to all the users
                }
                if(msg == "Disconnect")
                {
                msg=null;
                _CreatorP.RemoveClient(_client);
                }
                }*/
                while ((m = (Message) in.readObject()) != null)
                {
                    if (in != null)
                    {
//                    System.out.println(""+_client.GetName()+":" + m);
//                    _CreatorP.SendMessage(_client, m.GetString());// send the message to all the users
                        System.out.println("" + m.GetClientsrc() + ":" + m);
                        if (m.GetisSystemMessage())
                        {
                            _CreatorP._clientSockets[Integer.valueOf(m.GetString())].SetName(m.GetClientsrc());
                            System.out.println("client name"+_CreatorP._clientSockets[Integer.valueOf(m.GetString())].GetName());
                        }
                        else
                        {
                            _client.SetName(m.GetClientsrc());
                            _CreatorP.SendMessage(_client, m.GetString(), m.GetClientdst());// send the message to all the users
                        }
//                        _client.SetName(m.GetClientsrc());
//                        _CreatorP.SendMessage(_client, m.GetString()); // send the message to all the users
                    }
                    if (msg == "Disconnect") {
                        msg = null;
                        _CreatorP.RemoveClient(_client);
                    }
                }
            }
            catch (ClassNotFoundException ex)
            {
                Logger.getLogger(ServerMessageListener.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        catch (IOException ex)
        {
            Logger.getLogger(ServerMessageListener.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
